package cn.school.controller.workflow;

import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.exception.ApiException;
import cn.school.workflow.service.DocumentApplyService;
import cn.school.workflow.vo.DocumentApplyVO;
import cn.school.workflow.vo.WorkFlowVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 公文流转申请单
 */
@RestController
@RequestMapping("/documentApply")
@Api(tags = "公文流转接口")
public class DocumentApplyController {

    @Autowired
    private DocumentApplyService documentApplyService;
    /**
     * 查询公文信息
     * @return
     * @throws ApiException
     */
    @PostMapping("/findDocumentApplyPageList")
    @ApiOperation("list => 分页查询公文申请单")
    public CommonResult<CommonPage<DocumentApplyVO>> findLeaveApplyPageList(@RequestBody DocumentApplyVO vo) throws ApiException{
        IPage<DocumentApplyVO> pageList =  documentApplyService.findDocumentApplyPageList(vo);
        return  CommonResult.success(CommonPage.restPage(pageList));
    }

    @PostMapping("/cancelDocumentApply")
    @ApiOperation("cancel => 撤销公文申请")
    public CommonResult<CommonPage<DocumentApplyVO>> cancelDocumentApply(@RequestBody WorkFlowVO vo) throws ApiException{
        boolean flag =  documentApplyService.cancelDocumentApply(vo);
        if(flag){
            return CommonResult.success();
        }
        return  CommonResult.failed("流程取消失败");
    }

    @PostMapping("/createDocumentApply")
    @ApiOperation("create => 创建公文流程")
    public CommonResult<Integer> createDocumentApply(@RequestBody DocumentApplyVO  vo) throws ApiException{

        int flag =  documentApplyService.createDocumentApply(vo);
        if(flag>0){
            return CommonResult.success();
        }
        return  CommonResult.failed("流程创建失败");
    }

    @GetMapping("/findDocumentApplyDetail/{processInstanceId}")
    @ApiOperation("detail => 公文详情")
    public CommonResult<DocumentApplyVO> findDocumentApplyDetail(@PathVariable("processInstanceId") Long processInstanceId) throws ApiException{
        DocumentApplyVO documentApplyVO =  documentApplyService.findDocumentApplyDetail(processInstanceId);
        if(documentApplyVO!=null){
            return CommonResult.success(documentApplyVO);
        }
        return  CommonResult.failed("查询不单申请单");
    }
}
